package com.jquest.layer;

import com.jquest.data.Tile;
import com.jquest.data.Map;
import com.jquest.misc.ResourceMap;
import com.jquest.misc.Tileset;
import com.jquest.editor.Editor;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Color;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Rectangle;

/**
 * Created by IntelliJ IDEA.
 * User: Johan
 * Date: 2009-dec-11
 * Time: 17:23:38
 * To change this template use File | Settings | File Templates.
 */
public class TileLayer extends MapLayer {

    private Tile[][] tiles;
    public boolean EDITOR_MODE = false;

    private Map map;
    private Tileset ts;

    public TileLayer(Map owner, Tileset ts) {
        tiles = new Tile[owner.getHeight()][owner.getWidth()];
        this.map = owner;
        this.ts = ts;

        fillArray();
    }

    public Tileset getTileset() {
        return ts;
    }

    public void fillArray() {
        for(int x = 0; x < tiles.length; x++) {
            for(int y = 0; y < tiles[x].length; y++) {
                tiles[x][y] = new Tile(x, y, -1);
            }
        }
    }

    public TileLayer(Tile[][] tiles) {
        this.tiles = tiles;
    }

    public Tile[][] getTiles() {
        return tiles;
    }

    public void set(int x, int y, Tile t) {
        tiles[y][x] = t;
    }

    public void render(Graphics g) {
        if(isEnabled()) {
            for(int i = 0; i < tiles.length; i++) {
                for(int j = 0; j < tiles[i].length; j++) {
                    if(tiles[i][j] != null) {
                        tiles[i][j].render(g, ts);
                    }
                }
            }
            if(EDITOR_MODE) {
                g.setColor(Color.white);
                for(int i = 0; i < tiles.length; i++) {
                    for(int j = 0; j < tiles[i].length; j++) {
                        g.drawRect(i * Tile.TILE_WIDTH,  j * Tile.TILE_HEIGHT, Tile.TILE_WIDTH, Tile.TILE_HEIGHT);
                    }
                }
            }
        }
    }

    public void update(int time) {
        if(isEnabled()) {
            for(int i = 0; i < tiles.length; i++) {
                for(int j = 0; j < tiles[i].length; j++) {
                    if(tiles[i][j] != null)
                        tiles[i][j].update(time);
                }
            }
        }
    }

}
